package com.gitee.candy.other;

import java.util.*;

/**
 * @author ：suyanlong
 * @date ：Created in 2020/8/19 9:18
 * @description：路由负载均衡算法
 * @version: 1.0
 */
public class LoadBalance {
    /**
     * 服务器列表
     */
    private static HashMap<String, Integer> serverMap = new HashMap<>();
    public static List<String> serverList = new ArrayList<>();
    private static Integer index = 0;

    static {
        serverList.add("192.168.1.1");
        serverList.add("192.168.1.2");
        serverList.add("192.168.1.3");
        serverList.add("192.168.1.4");
        serverMap.put("192.168.1.2", 2);
        serverMap.put("192.168.1.3", 2);
        serverMap.put("192.168.1.4", 2);
        serverMap.put("192.168.1.5", 4);
    }

    /**
     * 随机访问
     */
    public static String random() {
        // 因为使用了非线程安全的集合，所以在访问操作时操作的是集合的拷贝
        List<String> tempList = new ArrayList<>(serverList.size());
        tempList.addAll(serverList);
        int randomInt = new Random().nextInt(tempList.size());
        return tempList.get(randomInt);
    }

    /**
     * 轮询算法
     */
    public static String randomOneByOne() {
        // 复制遍历用的集合，防止操作中集合有变更
        List<String> tempList = new ArrayList<>(serverList.size());
        tempList.addAll(serverList);
        String server = "";
        synchronized (index) {
            index++;
            if (index == tempList.size()) {
                index = 0;
            }
            server = tempList.get(index);
        }
        return server;
    }

    /**
     * 加权路由算法
     */
    public static String oneByOneWithWeight() {
        List<String> tempList = new ArrayList();
        HashMap<String, Integer> tempMap = new HashMap<>();
        tempMap.putAll(serverMap);
        for (String key : serverMap.keySet()) {
            for (int i = 0; i < serverMap.get(key); i++) {
                tempList.add(key);
            }
        }
        synchronized (index) {
            index++;
            if (index == tempList.size()) {
                index = 0;
            }
            return tempList.get(index);
        }
    }

    /**
     * 随机加权路由算法
     */
    public static String randomWithWeight() {
        List<String> tempList = new ArrayList();
        HashMap<String, Integer> tempMap = new HashMap<>();
        tempMap.putAll(serverMap);
        for (String key : serverMap.keySet()) {
            for (int i = 0; i < serverMap.get(key); i++) {
                tempList.add(key);
            }
        }
        int randomInt = new Random().nextInt(tempList.size());
        return tempList.get(randomInt);
    }

    /**
     * ip hash 路由算法
     */
    public static String ipHash(String ip) {
        // 复制遍历用的集合，防止操作中集合有变更
        List<String> tempList = new ArrayList<>(serverList.size());
        tempList.addAll(serverList);
        // 哈希计算请求的服务器
        int index = ip.hashCode() % serverList.size();
        return tempList.get(Math.abs(index));
    }

    public static void main(String[] args) {
        System.out.println("123456abcd".toUpperCase());
        System.out.println(ipHash("192.168.5.9"));
        HashMap<String, Integer> serverMap = new HashMap<>();
        for (int i = 0; i < 20000; i++) {
            String server = randomWithWeight();
            Integer count = serverMap.get(server);
            if (count == null) {
                count = 1;
            } else {
                count++;
            }
            // 记录
            serverMap.put(server, count);
        }
        // 路由总体结果
        for (Map.Entry<String, Integer> entry : serverMap.entrySet()) {
            System.out.println("IP:" + entry.getKey() + "，次数：" + entry.getValue());
        }
    }
}
